package template;

import java.io.Serializable;
import java.sql.SQLException;

import net.databinder.auth.AuthSession;
import net.databinder.auth.components.ao.DataUserStatusPanel;
import net.databinder.components.ao.DataForm;
import net.java.ao.DatabaseProvider;
import net.java.ao.EntityManager;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 * Page that adds a template border for layout.
 * 
 */
public class CreateCalendar extends Restricted
{
	//default message shown in text field on CreateCalendar page
	private String inputMessage = "";	
	
	//nested private class of Form, allows override of onSubmit() to
	//create the calendar in the database
	private class CalendarNameInput extends Form {
		//private class needs instance of containing class
		private CreateCalendar _createCalendar;
		
		//constructor to instantiate form and set CreateCalendar reference
		public CalendarNameInput(String id, CreateCalendar createCalendar) {
			
			//calls constructor of superclass Form
			super(id);
			
			_createCalendar = createCalendar;
		}
		//override of onSubmit() method to create calendar in DB
		public void onSubmit(){
			try {
				_createCalendar.createCalendar();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
     * Constructor
	 * 
     */
    public CreateCalendar()
    {
    	//PropertyModel for HTML class to reference with wicket:id's
    	PropertyModel inputMessageModel = new PropertyModel(this, "inputMessage");
    	
    	//form that contains text field to input Calendar name
    	CalendarNameInput form = new CalendarNameInput("form", this);
    	
    	//form adds TextField for input of CalendarName
    	form.add(new TextField("input", inputMessageModel));
    	    	
    	//layout adds the form
    	_layout.add(form);   
    }
    //accessor method for retrieving the current value of inputMessage
    public String getMessage() {
    	return inputMessage;
    }
    //method to create calendar in the DB, called from onSubmit() override
    public void createCalendar() throws SQLException {
    	//retrieves current value of inputMessage
    	String name = this.getMessage();
    	
    	//if the name field was left blank, return the user to the page
    	//with an error notifying them that they must enter a name
    	if(name == null) {
    		this.setResponsePage(CreateCalendarNullNameError.class);
    		return;
    	}
    	
    	//gets instance of the EntityManager to allow insertion into DB
    	EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
    	
    	//defines the user in the current session
    	Person user = (Person)_session.getUser();
    	
    	//retrieves all calendars belonging to the user
    	Calendar[] userCalendars = manager.find(Calendar.class, "creatorID = ?", user.getID());
    	
    	//determines if a calendar with that name already exists for the user
    	//if so, direct to error page
    	for(int i=0;i<userCalendars.length;i++) {
    		String tempName = userCalendars[i].getName();
    		if(tempName.equals(name)) {
    			this.setResponsePage(CreateCalendarError.class);
    			return;
    		}
    	}
    	
    	//manager creates instance of Calendar in DB
    	Calendar calendar = manager.create(Calendar.class);
    	
    	//sets calendar name to inputMessage
    	calendar.setName(name);
    	
    	//set calendar creator to current user
    	calendar.setCreator(user);
    	
    	//calendar gets saved in DB
    	calendar.save();
    	
    	//sets response page to redirect user after they click submit
    	this.setResponsePage(ViewCalendars.class);
    }
    public void setMessage(String message) {
    	inputMessage = message;
    }
}